import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:like_button/like_button.dart';
import 'package:qgchat/common/class/date/index.dart';
import 'package:qgchat/common/class/eventBus/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/common.dart';
import 'package:qgchat/http/models/dongtaiInfoModel.dart';
import 'package:qgchat/http/models/resModel.dart';
import 'package:qgchat/widgets/due/dif.dart';
import 'package:qgchat/widgets/dynamic/comment.dart';
import 'package:qgchat/widgets/dynamic/squareImgs.dart';
import 'BloC/dynamicBloC.dart';
import 'component/dynamicInput.dart';
import 'component/commentlist.dart';
import 'component/follow.dart';

class DynamicView extends StatefulWidget {
  DynamicView({Key key}) : super(key: key);

  @override
  _DynamicViewState createState() => _DynamicViewState();
}

class _DynamicViewState extends State<DynamicView> {
  @override
  void initState() {
    super.initState();
    BlocProvider.of<DynamicBloc>(context).add(DynamicEvent.refresh);
  }

  /// 点赞
  Future<bool> like(bool isLiked, int id) async {
    ResModel res;
    if (isLiked) {
      res = await CommonApis.canceldtdz(id);
    } else {
      res = await CommonApis.dtdz(id);
    }
    return res.status == 1 && !isLiked;
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<DynamicBloc, DongtaiInfoModel>(
      builder: (context, state) {
        if (state.memberNickname == null) {
          return Scaffold();
        }
        return Scaffold(
          appBar: PreferredSize(
            preferredSize: Size.fromHeight(px(88)),
            child: AppBar(
              centerTitle: true,
              title: Text(
                "她的动态",
                style: TextStyle(
                  color: Colors.black,
                  fontSize: px(34),
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          body: Container(
            color: col("f5f7fa"),
            child: Stack(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(bottom: px(98)),
                  child: ListView(
                    children: <Widget>[
                      // 动态内容
                      Container(
                        color: Colors.white,
                        margin: EdgeInsets.only(bottom: px(15)),
                        padding: EdgeInsets.all(px(30)),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            // 头像 昵称 时间 | 关注按钮
                            Container(
                              height: px(70),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  // 头像 昵称 时间
                                  Container(
                                    child: Row(
                                      children: <Widget>[
                                        ClipOval(
                                          child: Container(
                                            width: px(70),
                                            height: px(70),
                                            color: col("e4007f"),
                                            child: CachedNetworkImage(
                                              imageUrl: "${state.memberHead}",
                                              fit: BoxFit.cover,
                                              errorWidget:
                                                  (context, url, error) {
                                                return Config.errorNetImage;
                                              },
                                            ),
                                          ),
                                        ),
                                        Container(
                                          margin: EdgeInsets.only(left: px(20)),
                                          child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: <Widget>[
                                              Text(
                                                "${state.memberNickname}",
                                                style: TextStyle(
                                                  color: col("303133"),
                                                  fontSize: px(30),
                                                ),
                                              ),
                                              Text(
                                                "${Date.simple(int.parse(state.createtime))}",
                                                style: TextStyle(
                                                  height: .99,
                                                  color: col("bfc2cc"),
                                                  fontSize: px(24),
                                                ),
                                              ),
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                  // 关注按钮
                                  DynamicFollow(
                                    state.type == 1,
                                    state.uid,
                                  ),
                                ],
                              ),
                            ),
                            // 配文
                            If(
                              dif: state.text != null,
                              ifView: Container(
                                margin: EdgeInsets.only(top: px(20)),
                                child: Text(
                                  "${state.text}",
                                  style: TextStyle(
                                    color: col("303133"),
                                    fontSize: px(30),
                                  ),
                                ),
                              ),
                            ),
                            // 配图
                            Container(
                              margin: EdgeInsets.only(top: px(20)),
                              child: SquareImgs(imgs: state.img),
                            ),
                            // 点赞数 评论数
                            Container(
                              margin: EdgeInsets.only(top: px(20)),
                              child: Row(
                                children: <Widget>[
                                  LikeButton(
                                    isLiked: state.zan == 1,
                                    onTap: (bool isLiked) async {
                                      return await like(isLiked, state.id);
                                    },
                                    likeBuilder: (bool isLiked) {
                                      String likesUrl =
                                          "assets/images/icon/likes.png";
                                      String nolikesUrl =
                                          "assets/images/icon/nolikes.png";
                                      return Image.asset(
                                        "${isLiked ? likesUrl : nolikesUrl}",
                                        width: px(32),
                                        height: px(30),
                                      );
                                    },
                                    likeCount: state.memberFollow,
                                    size: px(32),
                                  ),
                                  SquareComment(
                                    state.comcount,
                                    onPressed: () {
                                      bus.emit("dtfocus", {
                                        "pid": null,
                                        "nickname": state.memberNickname,
                                      });
                                    },
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      // 评论列表
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.symmetric(
                          horizontal: px(30),
                          vertical: px(40),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            // 评论总数
                            Text(
                              "共${state.comcount}条评论",
                              style: TextStyle(
                                color: col("909399"),
                                fontSize: px(24),
                              ),
                            ),
                            // 评论列表
                            CommentList(),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Positioned(
                  bottom: 0,
                  // 输入框
                  child: DynamicInput(state.id),
                )
              ],
            ),
          ),
        );
      },
    );
  }
}
